<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'basic';
/*******basic*******/
if($_RQ['op']=='basic'){
	$pid=pdo_getcolumn('core_plugins',array('name'=>'jy_merchscrm'),'id');
	$item=pdo_get('core_open_plugin',array('pid'=>$pid,'uniacid'=>$_SESSION['uniacid'],'isplugin'=>0));
	$code=$_RQ['code']?:$_RQ['auth_code'];
	if($code){
		$content=model('core/wxwork_api',$item['id'])->get_permanent_code($code);
		$merchid=$_RQ['merchid']?:$_SESSION['onemerchid'];
		model('jy_merch/wxwork')->upitem($merchid,$content,'',$pid);
	}
	$return['up2workurl']=custom_url('/merch.authapp/up2workurl/',true);
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='up2workurl'){
	$pid=pdo_getcolumn('core_plugins',array('name'=>'jy_merchscrm'),'id');
	$item=pdo_get('core_open_plugin',array('pid'=>$pid,'uniacid'=>$_SESSION['uniacid'],'isplugin'=>0));
	if($item['id']){
		$pre_auth_code=model('core/wxwork_api',$item['id'])->get_pre_auth_code();
		model('core/wxwork_api',$item['id'])->set_session_info($pre_auth_code);
	}
	$backurl=urlencode(custom_url('jy_merchscrm/merch.authapp//merchid='.$_SESSION['onemerchid'],true));
	$state=session_id();
	$state.='merchid_'.$_SESSION['onemerchid'];
	$up2workurl='https://open.work.weixin.qq.com/3rdapp/install?suite_id='.$item['suite_id'].'&pre_auth_code='.$pre_auth_code.'&redirect_uri='.$backurl.'&state='.$state;
	$return['authurl']=$up2workurl;
	$return['pre_auth_code']=$pre_auth_code;
	exi('','',$up2workurl);
}
